﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using WF_TheTourManagement.ControllerClass;

namespace WF_TheTourManagement
{
    public partial class TourManagement : Form
    {
        public TourManagement()
        {
            InitializeComponent();
        }

        SqlConnection connection;
        DataTable dataTable = new DataTable("Tours");
        SqlDataAdapter dataAdapter = new SqlDataAdapter();

        private void connect()
        {
            string conString = "Data Source=.\\SQLEXPRESS;Initial Catalog=ToursManagement;Integrated Security=True";
            //string cmdString = "select * from Tours";

            try
            {
                connection = new SqlConnection(conString);
                connection.Open();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error - Unable to connect");
            }


        }

        void disconnect()
        {
            connection.Close();
            connection.Dispose();
            connection = null;
        }

        private void getAllLocations()
        {
            DataTable dataTable = new DataTable("Tours");
            SqlDataAdapter dataAdapter = new SqlDataAdapter();

            SqlCommand sqlCmd = new SqlCommand();
            sqlCmd.Connection = connection;

            //Cach 1: dung cau van tru tiep
            //sqlCmd.CommandType = CommandType.Text;
            //sqlCmd.CommandText = @"select * from names";
            //da.SelectCommand = sqlCmd;
            //da.Fill(dt);

            //Cach 2: thuc hien thong qua Stored Procedure
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "getAllLocations";
            SqlDataReader daReader = sqlCmd.ExecuteReader();
            dataTable.Load(daReader);

            lbxAllLocations.DisplayMember = "LocationDescription";
            lbxAllLocations.ValueMember = "LocationId";
            lbxAllLocations.DataSource = dataTable;


        }
        private void getData()
        {
            SqlCommand sqlCmd = new SqlCommand();
            sqlCmd.Connection = connection;

            //Cach 1: dung cau van tru tiep
            //sqlCmd.CommandType = CommandType.Text;
            //sqlCmd.CommandText = @"select * from names";
            //da.SelectCommand = sqlCmd;
            //da.Fill(dt);

            //Cach 2: thuc hien thong qua Stored Procedure
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "getTours";
            SqlDataReader daReader = sqlCmd.ExecuteReader();
            dataTable.Load(daReader);


            dgvTours.DataSource = dataTable;
            DataGridViewColumn d = dgvTours.Columns[0];
            d.Width = 40;
            d = dgvTours.Columns[2];
            d.Width = 60;

            getAllLocations();
        }

        void binding()
        {
            txtTourId.DataBindings.Clear();
            txtTourId.DataBindings.Add("Text", dgvTours.DataSource, "TourId");
            txtTourName.DataBindings.Clear();
            txtTourName.DataBindings.Add("Text", dgvTours.DataSource, "TourName");
            cbTourType.DataBindings.Clear();
            cbTourType.DataBindings.Add("ValueMember", dgvTours.DataSource, "TourType");

            txtTotalPrize.DataBindings.Clear();
            txtTotalPrize.DataBindings.Add("Text", dgvTours.DataSource, "CurrentPrize");

            
        }
        TourDetailsManagement tdm = new TourDetailsManagement();
        private void TourManagement_Load(object sender, EventArgs e)
        {
            
            tdm.initAll();
            rebind();
            //connect();
            //getData();
            //binding();
        }
        public void rebind()
        {
            bindType();
            bindTour();
            bindLocationsList();
            bindDetails();
        }
        void bindDetails()
        {
            txtTourId.DataBindings.Clear();
            txtTourId.DataBindings.Add("Text", dgvTours.DataSource, "TourId");
            txtTourName.DataBindings.Clear();
            txtTourName.DataBindings.Add("Text", dgvTours.DataSource, "TourName");
            //cbTourType.DataBindings.Clear();
            //cbTourType.DataBindings.Add("ValueMember", dgvTours.DataSource, "TourType");

            txtTotalPrize.DataBindings.Clear();
            txtTotalPrize.DataBindings.Add("Text", dgvTours.DataSource, "CurrentPrize");
            
        }

        void bindTour()
        {
            dgvTours.DataSource = tdm.allTours;
        }

        void bindType()
        {
            cbTourType.DataSource = tdm.allTourTypes;
            cbTourType.DisplayMember = "TypeName";
            cbTourType.ValueMember = "TypeId";
        }

        void bindLocationsList()
        {
            lbxAllLocations.DataSource = tdm.allLocations;
            lbxAllLocations.DisplayMember = "LocationName";
            lbxAllLocations.ValueMember = "LocationId";
        }
        

        private void btnNew_Click(object sender, EventArgs e)
        {
            dgvTours.Enabled = false;
            btnUpdateTour.Enabled = false;
            btnDeleteTour.Enabled = false;
            txtSearchKeyWord.Enabled = false;
            btnSearch.Enabled = false;

            txtTourId.Text = "Will be created later";
            txtTourName.Clear();
            txtTotalPrize.Clear();
            cbTourType.ResetText();
            

        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            MessageBox.Show("Function Not Implemented");
        }

        private void btnTry_Click(object sender, EventArgs e)
        {
            //MainProgram mp = new MainProgram();
            //mp.Show();
            
            LocationManament lm = new LocationManament();
            lm.Show();
            this.Hide();
            //this.Dispose();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            dgvTours.Enabled = true;
            btnUpdateTour.Enabled = true;
            btnDeleteTour.Enabled = true;
            txtSearchKeyWord.Enabled = true;
            btnSearch.Enabled = true;

            TourDetailsManagement tdm = new TourDetailsManagement();
            tdm.tourName = txtTourName.Text;

            tdm.saveAll();


        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            //disconnect();
        }

        private void btnResetAll_Click(object sender, EventArgs e)
        {
            txtTourName.Clear();
            txtTotalPrize.Clear();
            cbTourType.ResetText();
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        
    }
}
